package it.uniroma2.controller;

import it.uniroma2.cloud.PROVIDER;

import com.yammer.metrics.Gauge;
import com.yammer.metrics.MetricRegistry;

public class InstanceCounterMonitor {

	private CloudConfig cloudConfig; 
	
	public InstanceCounterMonitor(CloudConfig cloudConfig) {
		this.cloudConfig = cloudConfig;
		initialize();
	}

	private void initialize() {
		ControllerContextListener.mRegistry.register(
				MetricRegistry.name(PROVIDER.OPENSTACK.name() + "." + "howManyInstances"),
				new Gauge<Integer>() {
					public Integer getValue() {
						int howManyWorkerNode = cloudConfig.getCloudstackCommand()
								.howManyRunningWorkerNodes();
						System.out.println(PROVIDER.OPENSTACK.name() + "." + "howManyInstances: " + howManyWorkerNode);
						ReporterChecker.getInstance().setInstancesNum(PROVIDER.OPENSTACK, howManyWorkerNode);
						return howManyWorkerNode;
					}
				});

		ControllerContextListener.mRegistry.register(
				MetricRegistry.name(PROVIDER.AWS_EC2.name() + "." + "howManyInstances"),
				new Gauge<Integer>() {
					public Integer getValue() {
						int howManyWorkerNode = cloudConfig.getAwsCommand()
								.howManyRunningWorkerNodes();
						System.out.println(PROVIDER.AWS_EC2.name() + "." + "howManyInstances: " + howManyWorkerNode);
						ReporterChecker.getInstance().setInstancesNum(PROVIDER.AWS_EC2, howManyWorkerNode);
						return howManyWorkerNode;
					}
				});
	}

}
